/**
 * 典型违规管理
 */
Ext.define('Module.wg.WgDxwg', {
	extend : 'Ext.panel.Panel',
	alias : 'widget.wgDxwg',
	layout: 'border',
	initComponent : function() {
		var me = this;
		var store=Ext.create('Ext.data.Store',{
			autoLoad: true,
			fields: ['id','dxwgName','describe','upuser','uptime','name'],
	        proxy: {
	            type: 'ajax',
	            url: 'wg/WgDxwgAction/getWgDxwg.do',
	            reader: {
					root: 'datas',
					totalProperty: 'total'
				}
	        }
		});
		
		
		var WgDxwgPanel = me.WgDxwgPanel = Ext.create('Ext.grid.Panel',{
			title: '典型违规信息列表',
			region: 'center',
			padding: '0 1 0 0',
//			width: 650,
			store: store,
			tbar:[{
				xtype: 'panel',
				border: false,
				height: 25,
				width: '100%',
				bodyPadding: 0-1,
				layout: 'column',
				defaults: {columnWidth: 1,bodyPadding: 0},
				items: [{
						xtype: 'toolbar',
						items: [{
							text: '添加信息',
							iconCls: 'icon-add',
							handler: function(){
								me.addWgDxwg();
							}
						},{
							text: '修改信息',
							iconCls: 'icon-edit',
							handler: function(){
								var rec = WgDxwgPanel.getSelectionModel().getSelection()[0];
								if(rec){
									me.updateWgDxwg(rec);
//									console.debug(rec)
								}
								else{
									Ext.Msg.alert('提示', '请选择要修改的信息！');
								}
							}
						},{
							text: '删除信息',
							iconCls: 'icon-delete',
							handler: function(){
								var rec = WgDxwgPanel.getSelectionModel().getSelection()[0];
								if(rec){
									me.delWgDxwg(rec);
								}
								else{
									Ext.Msg.alert('提示', '请选择要删除的信息！');
								}
							}
						},{
							xtype:"textfield",
							fieldLabel : '典型违规名称',	
							regex: /^[a-zA-Z0-9\u4e00-\u9fa5]+$/,
							regexText : '不能输入特殊字符',
							maxLength:40,
							labelWidth:90,
							width:220
						},{
							text:"查询",
							iconCls: 'icon-search',
							handler: function(){
								var tbar = this.up('toolbar');
								var items = tbar.items.items;
								WgDxwgPanel.store.proxy.extraParams = {
									dxwgName: items[3].getSubmitValue()
								};
								WgDxwgPanel.store.loadPage(1);
							}
						}
					]
					 }]
			}],		
					
					
			bbar: Ext.create('Ext.PagingToolbar', {
	            store: store,
	            id: me.id+'-bbar',
	            displayInfo: true,
	            displayMsg: '显示第 {0} 至 {1} 条记录，共 {2} 条',
	            emptyMsg: "无记录"
	        }),
			columns: [{
	            text: '典型违规名称',
	            flex: 1.5,
	            dataIndex: 'dxwgName'
			},{
	            text: '描述',
	            flex: 2.5,
	            dataIndex: 'describe'
			},{
	            text: '更新用户',
	            flex: 1.6,
	            dataIndex: 'name'
			},{
	            text: '更新时间',
	            flex: 1.6,
	            dataIndex: 'uptime',
	            renderer: formatDate
			}]
		});
		
		
		me.items = [WgDxwgPanel];
		
		me.callParent();
	},
	updateWgDxwg: function(rec){
		var me = this;
		Ext.create('Ext.window.Window',{
			width: 390,
			height: 190,
			iconCls: 'icon-edit',
			modal: true,
			layout: 'fit',
			title: '修改信息',
			buttons: [{
				text: '保存',
				handler: function(){
					var formp = this.up('panel').items.items[0];
        	    	var form = formp.getForm();
        	    	if(form.isValid()){
        	    		form.submit({
            	    	    url: 'wg/WgDxwgAction/updateWgDxwg.do',
            	    	    success: function(form, action) {
            	    	    	var rs = Ext.decode(action.response.responseText);
            	    	    	if(rs.rst){
            	    	    		Ext.Msg.alert('提示', '修改成功！');
            	    	    		formp.up('window').close();
            	    	    		me.WgDxwgPanel.store.load();
            	    	    	}
            	    	    	else{
            	    	    		Ext.Msg.alert('提示', '修改失败！详情：<br>'+rs.msg);
            	    	    	}
            	    	    }
            	    	});
        	    	}
        	    	else{
        	    		Ext.Msg.alert('提示', '无效的信息,请正确填写信息！');
        	    	}
				}
			},{
				text: '取消',
				handler: function(){
					this.up('window').close();
				}
			}],
			items: me.getForm({
				bodyPadding: 20,
				border: false,
				listeners: {
					afterrender: function(fp){
						fp.loadRecord(rec||{});
					}
				}
			})
		}).show();
	},
	addWgDxwg: function(rec){
		var me = this;
		Ext.create('Ext.window.Window',{
			width: 390,
			height: 190,
			iconCls: 'icon-add',
			modal: true,
			layout: 'fit',
			title: '添加信息',
			buttons: [{
				text: '保存',
				handler: function(){
					var formp = this.up('panel').items.items[0];
        	    	var form = formp.getForm();
        	    	if(form.isValid()){
        	    		form.submit({
            	    	    url: 'wg/WgDxwgAction/addWgDxwg.do',
            	    	    success: function(form, action) {
            	    	    	var rs = Ext.decode(action.response.responseText);
            	    	    	if(rs.rst){
            	    	    		Ext.Msg.alert('提示', '保存成功！');
            	    	    		formp.up('window').close();
            	    	    		me.WgDxwgPanel.store.load();
            	    	    	}
            	    	    	else{
            	    	    		Ext.Msg.alert('提示', '保存失败！详情：<br>'+rs.msg);
            	    	    	}
            	    	    }
            	    	});
        	    	}
        	    	else{
        	    		Ext.Msg.alert('提示', '无效的信息,请正确填写信息！');
        	    	}
				}
			},{
				text: '取消',
				handler: function(){
					this.up('window').close();
				}
			}],
			items: me.getForm({
				bodyPadding: 20,
				border: false,
				listeners: {
					afterrender: function(fp){
						fp.loadRecord(rec||{});
					}
				}
			})
		}).show();
	},
	delWgDxwg: function(rec){
		var me = this;
		if(rec){
			Ext.Msg.confirm('提示', '你确定要删除当前信息吗？',function(b){
				if('yes' == b){
					Ext.Ajax.request({
					    url: 'wg/WgDxwgAction/deleteWgDxwg.do',
					    params: rec.data,
					    success: function(response){
        	    	    	var rs = Ext.decode(response.responseText);
        	    	    	if(rs.rst){
        	    	    		Ext.Msg.alert('提示', '删除成功！');
        	    	    		me.WgDxwgPanel.store.load();
        	    	    	}
        	    	    	else{
        	    	    		Ext.Msg.alert('提示', '删除失败！详情：<br>'+rs.msg);
        	    	    	}
					    }
					});
				}
			});
		}
		else{
			Ext.Msg.alert('提示', '请选择要删除的信息！');
		}
	},
	getForm: function(config,readOnly){
		return Ext.apply({
			xtype: 'form',
			bodyPadding: 10,
			defaults: {anchor: '100%'},
			items: [{
					name: 'id',
					xtype: 'hidden'
				},{
					fieldLabel: '典型违规名称',
					name: 'dxwgName',
					xtype: 'textfield',
					regex: /^[a-zA-Z0-9\u4e00-\u9fa5]+$/,
					regexText : '不能输入特殊字符',
					maxLength:50,
					allowBlank: false
				},{
					xtype: 'textarea',
					fieldLabel: '描述',
					name: 'describe',
					maxLength:100
			}]
		},config);
	}
});